// const app = getApp();
const app = getApp();
Page({
  data: {
    req: false,
    StatusBar: 0,
    CustomBar: 0,
    hidden: true,
    rows: [
      [{
        word: 'A',
        name: 'aaa'
      }],
      [{
        word: 'B',
        name: 'bbb'
      }, {
        word: 'B',
        name: 'b88'
      }],
      [{
        word: 'C',
        name: 'ccc'
      }],
      [{
        word: 'D',
        name: 'ddd'
      }],
      [{
        word: 'E',
        name: 'eee'
      }],
      [{
        word: 'F',
        name: 'fff'
      }],
      [{
        word: 'G',
        name: 'ggg'
      }],
      [{
        word: 'X',
        name: 'xxx'
      }],
      [{
        word: 'Y',
        name: 'yyy'
      }]
    ],
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    friendList: []
  },
  call(e){
    var dataset = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: dataset.tel 
    })
  },
  bindscroll(e) {
    // this.setData({
    //   num:null
    // })
  },
  friendList: function(append) {
    var that = this;
    that.toggle("req", false);
    var param = that.data.param
    app.req({
      url: 'orgmini/FriendVisitor/friendList',
      data: param,
      success: function(res) {
        if (200 == res.code) {
         
          var nowList = that.data.friendList;
          var friendList = res.data.rows;
          that.toggle("friendList", append ? nowList.concat(friendList) : friendList);
          that.toggle("param.totalPages", res.data.totalPages);
          that.toggle("req", true);
        }
      }
    });
  },
  delFriend(e) {
    var dataset = e.currentTarget.dataset;
    var that = this;
    wx.showModal({
      content: '是否要将' + dataset.name +'从您的好友列表中移除',
      confirmColor:'#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: 'orgmini/FriendVisitor/removeFriends',
            data: {
              friendId: dataset.id
            },
            success: function (res) {
              if (200 == res.code) {
                wx.showToast({
                  title: '删除好友成功'
                })
                that.friendList()
              } else {
                that.friendList()
              }
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    if (dataset.type) {
      url = url + "?type=" + dataset.type + '&id=' + dataset.id + '&name=' + dataset.name
    }
    wx.navigateTo({
      url: url
    })
  },
  onLoad() {
    let list = [];
    for (let i = 0; i < 26; i++) {
      list[i] = String.fromCharCode(65 + i)
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function(res) {
      that.setData({
        boxTop: res.top,
        boxheight: res.height
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function(res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  refreshList: function() {
    var that = this
    app.getUserInfo(function(userInfo) {
      that.toggle("param.uid", userInfo.id);
      that.toggle("param.pageNumber", 1);
      that.toggle("param.totalPages", 0);
      that.friendList()
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshList();
  },
  //获取文字信息
  getCur(e) {
    var that = this
    var arr = that.data.rows.filter(function(item) {
      return item[0].word.charCodeAt(0) == e.target.id * 1 + 65
    });
    if (arr.length) {
      that.setData({
        hidden: false,
        listCur: that.data.list[e.target.id],
        num: e.target.id
      })
    } else {
      var charCode = e.target.id * 1 + 65
      for (var i = 0; i < 26; i++) {
        --charCode
        var same = that.data.rows.filter(function(item) {
          console.log(item[0].word.charCodeAt(0), charCode)
          return item[0].word.charCodeAt(0) == charCode
        });
        if (same.length) {
          var i = same[0][0].word.charCodeAt(0) - 65
          that.setData({
            hidden: false,
            listCur: that.data.list[i],
            num: i
          })
          return
        }
      }
      console.log()
    }

  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      offsetheight = this.data.boxheight,
      windowHeight = wx.getSystemInfoSync().windowHeight,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < offsettop + offsetheight) {
      let num = parseInt(((y - offsettop) / offsetheight) * 26);
      this.setData({
        listCur: that.data.list[num],
        num: num
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i],
          movableY: i * 20
        })
        return false
      }
    }
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 5 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
      console.log(e.currentTarget.dataset)
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshList();
  },
  onReachBottom: function() {
    var param = this.data.param;
    if (param.pageNumber < page.totalPages) {
      this.toggle("param.pageNumber", param.pageNumber * 1 + 1);
      this.friendList(true);
    }
  }

});